<template>
  <div>
    <el-row class="mt-40" justify="end" type="flex">
      <el-form :model="deptForm" ref="deptForm" style="width: 100%">
        <el-col :span="4">
          <el-form-item>
            <el-input
              placeholder="部门名称"
              v-model="deptForm.deptname"
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :offset="1" :span="4">
          <el-form-item>
            <el-input
              placeholder="部门编号"
              v-model="deptForm.deptnum"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item align="right">
            <el-button type="primary" @click="deptHandle(1)" size="small"
              >查询</el-button
            >
          </el-form-item>
        </el-col>
        <el-col :span="3" :offset="10" align="right">
          <el-form-item>
            <el-button type="primary" size="small" @click="handleAdd()">
              <i class="el-icon-plus"></i>
              新增部门
            </el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-row>
    <!-- 数据-->
    <el-row>
      <el-col :span="24">
        <el-table :data="tableData" class="table">
          <el-table-column align="center" label="序号" width="220" type="index">
          </el-table-column>
          <el-table-column align="center" label="部门名称" width="220">
            <template slot-scope="scope">
              {{ scope.row.deptname }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="部门编号" width="220">
            <template slot-scope="scope">
              {{ scope.row.deptnum }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="备注信息" width="220">
            <template slot-scope="scope">
              {{ scope.row.remark }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="220">
            <template slot-scope="scope">
              <el-button size="small" @click="handleAdd(scope.row)">
                修改
              </el-button>
              <el-button
                type="primary"
                size="small"
                @click="handleDel(scope.row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>
    <!--分页-->
    <el-row class="mt-40" type="flex" justify="end">
      <el-pagination
        :current-page="pages.curPage"
        :page-size="10"
        :page-sizes="[5, 10]"
        :total="pages.totalCount"
        @current-change="handleCurrentChange"
        layout="total,sizes,prev,pager,next,jumper"
      />
    </el-row>
    <!--model-->
    <deptart-models
      :dialog="dialog"
      :deptFormModel="deptFormModel"
      @update="deptHandle"
    ></deptart-models>
  </div>
</template>
<script lang="ts">
import { Vue, Component } from "vue-property-decorator";
import { DeptForm, Pages, Dialog } from "@/interface/interface";
import { deptData, deldept } from "@/api/api";
import DeptartModels from "@/views/rightsmanagement/permissmodels/deptartModels.vue";
import { Notification } from "element-ui";
@Component({
  components: { DeptartModels }
})
export default class Depart extends Vue {
  protected name!: "DepartMent";
  protected deptForm: DeptForm = {
    deptname: "",
    deptnum: ""
  };
  protected pages: Pages = {
    totalCount: 10,
    curPage: 1,
    pageNum: 1
  };
  protected deptFormModel: DeptForm = {
    deptname: "",
    remark: ""
  };
  protected dialog: Dialog = {
    isDialog: false,
    options: "",
    title: ""
  };

  protected tableData: any[] = [];

  protected async created() {
    const { pageNum } = this.pages as any;
    await this.deptHandle(pageNum);
  }

  protected async deptHandle(pageNum: number) {
    const { deptname: qdeptName, deptnum: qdeptNum } = this.deptForm as any;
    const res = await deptData({ qdeptName, qdeptNum, pageNum: pageNum });
    if (res) {
      const { object, success, message } = res;
      if (success) {
        if (object) {
          const { datas, ...pages } = object;
          this.tableData = datas;
          this.pages = pages;
        } else {
          this.$notify({
            title: "提示",
            message: message
          });
        }
      } else {
        this.$notify({
          title: "提示",
          message: message
        });
      }
    } else {
      this.$notify({
        title: "提示",
        message: "数据出错"
      });
    }
  }

  protected handleCurrentChange(val: number) {
    this.deptHandle(val);
  }
  protected async handleDel(index: any) {
    try {
      const { id } = index;
      const res = await deldept({ deptId: id });
      const { success, message } = res;
      if (success) {
        this.$message({
          type: "success",
          message: "删除成功"
        });
      }
    } catch (e) {
      this.$message({
        type: "error",
        message: "删除失败"
      });
    }
  }
  protected handleAdd(params: any) {
    if (params) {
      Object.assign(this.deptFormModel, params);
      this.dialog = {
        isDialog: true,
        options: "edit",
        title: "编辑信息"
      };
    } else {
      this.deptFormModel = {
        deptname: "",
        remark: ""
      };
      this.dialog = {
        isDialog: true,
        options: "add",
        title: "添加信息"
      };
    }
  }
}
</script>

<style lang="scss" scoped>
.table-main {
  width: 100%;
  border: 1px solid #959595;
  border-radius: 4px;
  box-sizing: border-box;
}
</style>
